<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @open="initEditor"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="新闻名称" prop="fileName">
        <el-input v-model="dataForm.fileName" placeholder="新闻名称"></el-input>
      </el-form-item>
      <el-form-item label="新闻来源" prop="newsSource">
        <el-input
          v-model="dataForm.newsSource"
          placeholder="新闻来源"
        ></el-input>
      </el-form-item>
      <el-form-item label="新闻封面" prop="coverPath">
        <single-upload
          ref="singleUpload"
          v-model="dataForm.coverPath"
        ></single-upload>
      </el-form-item>
      <el-form-item label="新闻详情" prop="newDetails">
        <div id="demo1"></div>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
// 引入 wangEditor
import wangEditor from "wangeditor";
import singleUpload from "@/components/upload/singleUpload.vue";
export default {
  components: { singleUpload },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        userId: this.$cookie.get("userId"),
        fileName: "",
        newsSource: "",
        coverPath: "",
        newDetails: "",
      },
      editor: null,
     
      dataRule: {
        fileName: [
          { required: true, message: "新闻名称不能为空", trigger: "blur" },
        ],
        newsSource: [
          { required: true, message: "新闻来源不能为空", trigger: "blur" },
        ],
        coverPath: [
          { required: true, message: "新闻封面不能为空", trigger: "blur" },
        ],
        newDetails: [
          { required: true, message: "新闻详情不能为空", trigger: "blur" },
        ],
      },
    };
  },
  beforeDestroy() {
    // 调用销毁 API 对当前编辑器实例进行销毁
    this.editor.destroy();
    this.editor = null;
  },

  methods: {
    initEditor() {
      this.$nextTick(() => {
        console.log(wangEditor);
        const editor = new wangEditor("#demo1");
        console.log(editor);
        // 配置 onchange 回调函数，将数据同步到 vue 中
        editor.config.onchange = (newHtml) => {
          this.dataForm.newDetails = newHtml;
        };
        // 创建编辑器
        editor.create();
        this.editor = editor;
      });
    },
    getEditorData() {
      // 通过代码获取编辑器内容
      let data = this.editor.txt.html();
      alert(data);
    },

    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/heartSelf/heartNews/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.userId = data.heartNews.userId;
              this.dataForm.fileName = data.heartNews.fileName;
              this.dataForm.newsSource = data.heartNews.newsSource;
              this.dataForm.coverPath = data.heartNews.coverPath;
              this.dataForm.newDetails = data.heartNews.newDetails;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/heartSelf/heartNews/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              userId: this.dataForm.userId,
              fileName: this.dataForm.fileName,
              newsSource: this.dataForm.newsSource,
              coverPath: this.dataForm.coverPath,
              newDetails: this.dataForm.newDetails,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
